package lab_2.windows;

import ProtocolXML.newmessage.Message;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import lab_2.Client;
import javax.swing.DefaultListModel;
import javax.swing.text.PlainDocument;


/**
 * Class implements UserInterface
 * @author pacific
 */
public class IMWindow extends JFrame {
    //Features of window
    protected static final int WINDOW_WIDTH = 300 ;
    protected static final int WINDOW_HEIGHT = 250 ;
    
    
    public IMWindow() {
        setLayout( new BorderLayout() ) ;
        
        JList contactList = new JList( getContactsModel() ) ;
        add( contactList, BorderLayout.EAST );
        add( getHistoryPane(), BorderLayout.CENTER ) ;

        //Locate "Status Bar"
        JPanel bottomPanel = new JPanel( new BorderLayout() ) ;
        bottomPanel.add( getStatusBar(), BorderLayout.SOUTH ) ;

        //Locate "Panel For Input"
        JPanel panelForInput = createPanelForInput() ;
        bottomPanel.add( panelForInput, BorderLayout.CENTER ) ;

        add( bottomPanel, BorderLayout.SOUTH );

        getInputArea().setLineWrap( false );
        getInputArea().setDocument( new PlainDocument() ) ;
        
        getSubmit().addActionListener( new ButtonSendListener() );

        setBounds( 200, 200, WINDOW_WIDTH, WINDOW_HEIGHT );
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setVisible( true );
    }


    /**Create "InputPanel" with TextaArea and Button*/
     private JPanel createPanelForInput() {
        JPanel panelForInput = new JPanel( new BorderLayout() ) ;

        JPanel panel1 = new JPanel( new BorderLayout() ) ;
        panelForInput.add( panel1, BorderLayout.CENTER ) ;

        panel1.add( getInputArea(), BorderLayout.CENTER ) ;        

        panel1 = new JPanel( new FlowLayout() ) ;
        panel1.add( getSubmit() ) ;

        panelForInput.add( panel1, BorderLayout.EAST ) ;

        return panelForInput ;
     }
    
    /**
     * Add companion for commenication in tabbed pane and Contact list
     * @param companion
     */
    public void addCompanion( Companion companion ) {
        getContactsModel().addElement( companion ) ;
        getHistoryPane().add( companion.toString(), companion ) ;
        unblockWindow() ;
    }


    /**
     * Find and delete Companion with thet UserID
     * @param userId - ID of user to delete
     */
    public void delCompanion( Companion companion ) {
        getContactsModel().removeElement( companion ) ;
        getHistoryPane().remove( companion ) ;
        if( getHistoryPane().getSelectedIndex() < 0 )
            blockWindow() ;
    }


    public void retryConnect() {
        getClient().setStateClient( Client.ClientState.CONNECTION ) ;
    }


    /**Function calledfor exit */
    public void exit() {
        dispose() ;
        getClient().setStateClient( Client.ClientState.EXIT ) ;
        System.exit(0);
    }


    
    //Show window wait of connection
    public void blockWindow() {
            getInputArea().setEditable(false) ;
            getSubmit().setEnabled(false) ;
    }
    public void unblockWindow() {
        getInputArea().setEditable(true) ;
        getSubmit().setEnabled(true) ;
    }
    

    public static void main(String args[]) {
        IMWindow window = new IMWindow() ;
        Client client = new Client( window ) ;
        window.setClient( client ) ;
        client.start();
       
        

    }


    /**
     * Listener for button "Send Message"
     */
    class ButtonSendListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            
            int selectedIndex = getHistoryPane().getModel().getSelectedIndex()  ;
            Companion companion = (Companion) getHistoryPane().getComponent( selectedIndex ) ;
            int idReceiver = companion.getUserID() ;
            
            String text = getInputArea().getText() ;

            //If input field empty, do nothing
            if( "".equals( text ) ) {
                return ;
            }
            
            Message msg = new Message() ;
            msg.msgIsToSending( idReceiver, text ) ;
            getClient().write( msg ) ;

            companion.deliverMessage( text, Companion.Sender.ThisUser ) ;
            getInputArea().setText( "" ) ;
        }
    }


    //Size for input area
    protected static final int TEXT_AREA_ROWS = 4 ;
    protected static final int TEXT_AREA_COLUMNS = 15 ;


    //-----Fields-----

    private Client client ;
    private JTextArea inputArea = new JTextArea(TEXT_AREA_ROWS, TEXT_AREA_COLUMNS) ;
    private JTabbedPane history = new JTabbedPane() ;
    private DefaultListModel contactsModel = new DefaultListModel() ;
    private JButton submit = new JButton("Отправить") ;
    private JLabel statusBar = new JLabel(" ", JLabel.LEFT) ;


    //-----GETTERS and SETTERS-----

    //  for "inputArea"
    protected final JTextArea getInputArea() {
        return inputArea;
    }
    //  for "history"
    protected final JTabbedPane getHistoryPane() {
        return history;
    }
    //  for contacts
    protected final DefaultListModel getContactsModel() {
        return contactsModel;
    }
    //  for "client"
    public final Client getClient() {
        return client;
    }
    private void setClient(Client client) {
        this.client = client;
    }
    // for "sibmit"
    private JButton getSubmit() {
        return submit;
    }
    // for "statusBar"
    public final JLabel getStatusBar() {
        return statusBar;
    }
}